package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.ConditionVO;
import cn.explink.dop.entity.vo.DistributeStrategyVO;
import cn.explink.dop.enums.pub.EnableEnum;
import cn.explink.dop.service.DistributeStrategyService;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.core.util.StringUtil;

@Controller
@RequestMapping("/distributeStrategyController")
public class DistributeStrategyController {
	@Autowired
	private DistributeStrategyService distributeStrategyService;

	@RequestMapping(params = "index")
	public ModelAndView dopMainIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		view.setViewName("dop/distributor/distributeStrategy");
		return view;
	}

	/**
	 * 添加数据
	 *
	 * @param resName
	 *            餐厅名
	 * @param request
	 *            请求参数
	 * @return 添加成功对象
	 */
	@RequestMapping(params = "create")
	@ResponseBody
	public AjaxJson create(String name, @RequestParam(value = "conditionStrList[]", required = false) List<String> conditionStrList, HttpServletRequest request) {

		DistributeStrategyVO distributeStrategyVO = new DistributeStrategyVO();
		distributeStrategyVO.setName(name);
		distributeStrategyVO.setIsEnable(EnableEnum.Enable.getValue());

		List<ConditionVO> conditionList = new ArrayList<ConditionVO>();

		for (int i = 0; i < conditionStrList.size(); i++) {
			String conditionId = conditionStrList.get(i).substring(0, conditionStrList.get(i).indexOf(":"));
			String conditionWeight = conditionStrList.get(i).substring(conditionStrList.get(i).indexOf(":") + 1);
			ConditionVO condition = this.distributeStrategyService.getConditionById(conditionId);
			condition.setId(null);
			condition.setWeight(Integer.parseInt(conditionWeight));
			condition.setPriority(conditionStrList.size() - i);
			conditionList.add(condition);
		}
		distributeStrategyVO.setConditionList(conditionList);
		String id = (String) this.distributeStrategyService.save(distributeStrategyVO);
		// 将其他的分单策略改为停用
		this.distributeStrategyService.stopdistributeStrategy(id);
		AjaxJson j = new AjaxJson();
		if (null != id) {
			j.setSuccess(true);
		}
		return j;
	}

	@RequestMapping(params = "modify")
	@ResponseBody
	public AjaxJson modify(String id, String name, @RequestParam(value = "conditionStrList[]", required = false) List<String> conditionStrList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			DistributeStrategyVO distributeStrategyVO = this.distributeStrategyService.get(DistributeStrategyVO.class, id);
			distributeStrategyVO.setName(name);
			List<ConditionVO> conditionList = new ArrayList<ConditionVO>();

			for (int i = 0; i < conditionStrList.size(); i++) {
				String conditionId = conditionStrList.get(i).substring(0, conditionStrList.get(i).indexOf(":"));
				String conditionWeight = conditionStrList.get(i).substring(conditionStrList.get(i).indexOf(":") + 1);
				ConditionVO condition = this.distributeStrategyService.getConditionById(conditionId);
				condition.setId(null);
				condition.setWeight(Integer.parseInt(conditionWeight));
				condition.setPriority(conditionStrList.size() - i);

				conditionList.add(condition);
			}
			distributeStrategyVO.setConditionList(conditionList);
			this.distributeStrategyService.updateEntitie(distributeStrategyVO);
			j.setSuccess(true);
			return j;
		} catch (Exception e) {
			// TODO: handle exception
			j.setSuccess(false);
			return j;
		}

	}

	@RequestMapping(params = "queryList")
	@ResponseBody
	public Map<String, Object> queryList(String name, int pageNum, int pageSize, HttpServletRequest request) {
		DistributeStrategyVO distributeStrategyVO = new DistributeStrategyVO();
		try {
			if (StringUtil.isNotEmpty(name)) {
				distributeStrategyVO.setName(name);
			}
			return this.distributeStrategyService.getListDataByPage(distributeStrategyVO, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<DistributeStrategyVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.distributeStrategyService.getListDataByPage(new DistributeStrategyVO(), pageNum, pageSize);
			List<DistributeStrategyVO> distributeStrategyVOList = (List<DistributeStrategyVO>) map.get("list");
			for (DistributeStrategyVO distributeStrategyVO : distributeStrategyVOList) {
				distributeStrategyVO.setId(distributeStrategyVO.getId());
			}
			return distributeStrategyVOList;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DistributeStrategyVO>();
		}
	}

	@RequestMapping(params = "getPresetCondition")
	@ResponseBody
	public List<ConditionVO> getPresetCondition(HttpServletRequest request) {
		try {
			return this.distributeStrategyService.getPresetCondition();
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ConditionVO>();
		}
	}

	/**
	 *
	 * @Title: stop
	 * @Description: 停用策略,可批量
	 * @author 孟凡岭
	 * @param @param idsList
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */

	@RequestMapping(params = "stop")
	@ResponseBody
	public AjaxJson stop(@RequestParam(value = "idsList[]", required = false) List<String> idsList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.distributeStrategyService.stop(idsList);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: open
	 * @Description: 启用策略，并将其他策略设为停用
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "open")
	@ResponseBody
	public AjaxJson open(String id, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.distributeStrategyService.open(id);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: delete
	 * @Description: 删除策略
	 * @author 孟凡岭
	 * @param @param id
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "delete")
	@ResponseBody
	public AjaxJson delete(@RequestParam(value = "idsList[]", required = false) List<String> idsList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.distributeStrategyService.deleteDis(idsList);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			j.setSuccess(false);
		}
		return j;
	}

}
